package com.example.anquan.config;


import com.example.anquan.Service.UserService;
import com.example.anquan.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.sql.DataSource;
import javax.swing.*;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
//        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
//        auth.inMemoryAuthentication().passwordEncoder(encoder)
//                .withUser("vip").password(encoder.encode("123")).roles("vip")
//                .and()
//                .withUser("张三").password(encoder.encode("123")).roles("common");
//    }
//    @Override
//    protected void configure(HttpSecurity http)throws Exception {
//        http.authorizeRequests()
//                .antMatchers("/index").permitAll()
//                .antMatchers("/common/**").hasRole("common")
//                .antMatchers("/vip/**").hasRole("vip")
//                .anyRequest().authenticated()
//                .and().formLogin()
//                .and().logout()
//                .and().rememberMe();
//

    @Autowired
    private DataSource dataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
        String userSQL="select username,password,id from user where username=?";
        String authoritySQL="select c.username,a.name from user c,role a,user_roles ca where ca.user_id=c.id and ca.roles_id=a.id and c.username=?";
        auth.jdbcAuthentication().passwordEncoder(encoder)
                .dataSource(dataSource)
                .usersByUsernameQuery(userSQL)
                .authoritiesByUsernameQuery(authoritySQL);
    }
    @Override
    protected void configure(HttpSecurity http)throws Exception {
        http.authorizeRequests()
                .antMatchers("/article").permitAll()
                .antMatchers("/back/**").hasRole("admin")
                .antMatchers("/vip/**").hasRole("vip")
                .anyRequest().authenticated();
        http.formLogin()
                .loginPage("/login").permitAll()
                .usernameParameter("username").passwordParameter("password")
                .defaultSuccessUrl("/")
                .failureUrl("/login?error");
        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/");
        http.rememberMe()
                .rememberMeParameter("rememberme")
                .tokenValiditySeconds(200);
    }

//@Autowired
//private UserService userService;
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth)throws
//            Exception {
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        auth.userDetailsService(userService).passwordEncoder(encoder);
//    }
}

